package com.dg.gaming.api.common;

import com.dg.gaming.api.common.msg.*;
import nanoxml.*;


/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public abstract class AbstractConnectionThread {
    /** All clients start initially with "connected" equal to false
     *  (although they can still recieve/transfer logon information). */
    protected boolean connected = false;
    /** Username of the client. */
    protected long userid;
    /** When the boolean loop becomes false the Thread finishes. */
    protected boolean loop = true;
    /**
     * This abstract method must be overwritten by a child which extends this
     * class.
     *
     * @param message       Communication as an XML object.
     * @throws TransmissionException  This is thrown if there is a problem parsing the String.
     */
    public abstract void parse(XMLElement message) throws TransmissionException;

    /**
     * This method is called to properly clean up after a client.
     */
    public abstract void cleanup();

    public AbstractConnectionThread() {
    }


    /**
     * Stop the loop.
     */
    public void stopLoop() {
        this.loop = false;
    }

    /**
     * Set boolean to specify that this client has connected sucessfully.
     */
    public void connect() {
        this.connected = true;
    }

    /**
     * Send a ITransmittable object to the output stream (could be server or
     * client).
     *
     * @param transObject
     */
    public abstract void send(ITransmittable transObject);

//    {
//        // Retrieve XMLElement from the object and flatten to a String.
//        String message = transObject.flatten().toString();
//
//        // Send down the socket to the receiving end
//        out.println(message);
//    }

    /**
     * Returns the username.
     *
     * @return  Username of client / server who created this thread.
     */
    public long getUserId() {
        return userid;
    }

    /**
     * Set the username.
     *
     * @param username  Username of client / server who created this thread.
     */
    public void setUsername(long userid) {
        this.userid = userid;
    }

}
